/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.util.bdfparser.parsers;

import com.comac.ins.isfd.util.FileUtils;
import com.comac.ins.isfd.util.bdfparser.entities.bdf.BDFEntity;
import com.comac.ins.isfd.util.bdfparser.entities.bdf.cards.BaseCard;
import com.comac.ins.isfd.util.bdfparser.entities.bdf.rawdata.ParseFileOptions;
import com.comac.ins.isfd.util.bdfparser.entities.bdf.rawdata.RawDataLine;
import com.comac.ins.isfd.util.bdfparser.entities.bdf.sections.BulkDataEntry;
import com.comac.ins.isfd.util.bdfparser.entities.bdf.sections.FileManagementStatement;
import com.comac.ins.isfd.util.bdfparser.enums.ContentTypeEnum;
import com.comac.ins.isfd.util.bdfparser.enums.DeckTypeEnum;
import com.comac.ins.isfd.util.bdfparser.output.BDFGetters;
import org.springframework.util.CollectionUtils;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * BDF文件解析器
 */
public class BDFParser {


    public BDFParser() {

    }

    /**
     * 读取 BDF 文件
     *
     * @param bdfFilePath BDF 文件路径
     * @param options     解析文件选项
     * @return 解析后的数据列表
     */
    public static BDFEntity readBDF(Path bdfFilePath,
                                    ParseFileOptions options) {
        BDFEntity bdf = new BDFEntity();
        // 读取并初步处理数据
        Map<String, List<RawDataLine>> parsedDataLines = BDFFileUtils.readBDFFile(bdfFilePath, options);
        // 处理文件管理
        List<RawDataLine> fileManagementLines = parsedDataLines.get(DeckTypeEnum.FILE_MANAGEMENT_STATEMENT.getType());
        FileManagementStatement fileManagementStatement = processFileManagementStatement(fileManagementLines);
        // 处理卡片数据
        List<RawDataLine> bulkDataLines = parsedDataLines.get(DeckTypeEnum.BULK_DATA_ENTRY.getType());
        BulkDataEntry bulkDataEntry = processBulkDataEntry(bulkDataLines);

        bdf.setParsedDataLines(parsedDataLines);
        bdf.setBulkDataEntry(bulkDataEntry);
        bdf.setFileManagementStatement(fileManagementStatement);

        bdf.setContentType(!CollectionUtils.isEmpty(fileManagementLines) ? ContentTypeEnum.INDEX.getValue() : ContentTypeEnum.NORMAL.getValue());

        return bdf;
    }

    public static BDFEntity readBDF(Path bdfFilePath) {
        ParseFileOptions options = new ParseFileOptions();
        options.setPunch(true);
        options.setEncoding(Optional.of(FileUtils.detectFileEncoding(bdfFilePath.toAbsolutePath().toString())));
        options.setReadIncludes(false);
        return readBDF(bdfFilePath, options);
    }

    /**
     * 处理文件管理
     */
    private static FileManagementStatement processFileManagementStatement(List<RawDataLine> fileManagementLines) {
        FileManagementStatement fileManagementStatement = new FileManagementStatement();
        fileManagementStatement.setIncludeStatements(BDFFileUtils.parseIncludeLines(fileManagementLines));
        return fileManagementStatement;
    }

    /**
     * 处理卡片列表
     */
    private static BulkDataEntry processBulkDataEntry(List<RawDataLine> rawDataLines) {
        BulkDataEntry bulkDataEntry = new BulkDataEntry();
        BasicCardUtils.parseCards(bulkDataEntry, rawDataLines);
        return bulkDataEntry;
    }


    /**
     * 读取 BDF 文件
     * 只返回 原始数据行 和 卡片数据的cartCount
     */
    public static BDFEntity readSimpleBDF(Path bdfFilePath,
                                          ParseFileOptions options) {
        BDFEntity bdf = new BDFEntity();
        // 读取并初步处理数据
        Map<String, List<RawDataLine>> parsedDataLines = BDFFileUtils.readBDFFile(bdfFilePath, options);
        bdf.setParsedDataLines(parsedDataLines);

        // 处理卡片数据
        List<RawDataLine> bulkDataLines = parsedDataLines.get(DeckTypeEnum.BULK_DATA_ENTRY.getType());
        BulkDataEntry bulkDataEntry = processBulkDataEntry(bulkDataLines);
        BulkDataEntry newBulkDataEntry = new BulkDataEntry();
        newBulkDataEntry.setCardCount(bulkDataEntry.getCardCount());
        bdf.setBulkDataEntry(newBulkDataEntry);

        return bdf;
    }

    /**
     * 读取 BDF 文件
     * 只返回 原始数据行
     */
    public static BDFEntity readSimpleBDF(Path bdfFilePath) {
        ParseFileOptions options = new ParseFileOptions();
        options.setPunch(true);
        options.setEncoding(Optional.of(FileUtils.detectFileEncoding(bdfFilePath.toAbsolutePath().toString())));
        options.setReadIncludes(false);
        return readSimpleBDF(bdfFilePath, options);
    }

    // 测试
    public static void main(String[] args) {
        String filename = "C:\\Users\\lijpt\\OneDrive\\Documents\\COMAC\\ISFD\\model_parsing\\bdf_file\\compare\\real\\model\\model\\Model_cruise.bdf";
//        String filename1 = "C:\\Users\\hxloo\\Desktop\\要素库项目\\模型库&结果库对比需求\\model-R1\\model\\P_load\\P_LOAD_FRONT_D20240129.BDF";
//        String filename2 = "C:\\Users\\hxloo\\Desktop\\要素库项目\\模型库&结果库对比需求\\model-R1\\model\\C919S_S80_R1_D20240229_LINK_SLAT_FL_26p5_1.bdf";
//        String filename3 = "C:\\Users\\hxloo\\Desktop\\要素库项目\\模型库&结果库对比需求\\model-R1\\model\\Model_mcheck1.bdf";

        Path bdfFilePath = Paths.get(filename).toAbsolutePath();
        ParseFileOptions options = new ParseFileOptions();
        options.setPunch(true);
        options.setEncoding(Optional.of("UTF-8"));
        options.setReadIncludes(false);
        BDFEntity bdf = readBDF(bdfFilePath, options);
        System.out.println("content type :" + bdf.getContentType());
        Map<String, List<BaseCard>> organizedCardList = BDFGetters.organizeCardListByName(bdf.getBulkDataEntry().getCardCount(), bdf.getBulkDataEntry().getCardList());

        System.out.println("END of test");
    }


}
